package com.heima.article.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.article.constants.ArticleConstants;
import com.heima.article.mapper.ApArticleConfigMapper;
import com.heima.article.mapper.ApArticleContentMapper;
import com.heima.article.mapper.ApArticleMapper;
import com.heima.article.service.ApArticleService;
import com.heima.common.cache.CacheService;
import com.heima.model.article.dtos.ArticleDto;
import com.heima.model.article.dtos.ArticleHomeDto;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.article.pojos.ApArticleConfig;
import com.heima.model.article.pojos.ApArticleContent;
import com.heima.model.article.vos.HotArticleVo;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.search.pojos.SearchArticleVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class ApArticleServiceImpl extends ServiceImpl<ApArticleMapper, ApArticle> implements ApArticleService {
	@Autowired
	private ApArticleMapper apArticleMapper;
	@Autowired
	private ApArticleConfigMapper apArticleConfigMapper;
	@Autowired
	private ApArticleContentMapper apArticleContentMapper;
	@Autowired
	private CacheService cacheService;
	
	
	@Override
	public ResponseResult saveArticle(ArticleDto articleDto) {
		if (articleDto == null) {
			return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
		}
		
		Long id = articleDto.getId();
		
		ApArticle apArticle = new ApArticle();
		
		BeanUtils.copyProperties(articleDto, apArticle);
		
		String content = articleDto.getContent();
		
		//判断ID是是否存在,如果存在,则修改,如果不存在则进行新增
		if (id == null) {
			
			//插入article表
			int insert = apArticleMapper.insert(apArticle);
			if (insert < 1) {
				return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR, "新闻文章插入失败");
			}
			
			//插入config表
			ApArticleConfig apArticleConfig = new ApArticleConfig();
			apArticleConfig.setIsDown(false);
			apArticleConfig.setIsDelete(false);
			apArticleConfig.setIsForward(true);
			apArticleConfig.setIsComment(true);
			apArticleConfig.setArticleId(apArticle.getId());
			int insert1 = apArticleConfigMapper.insert(apArticleConfig);
			if (insert1 < 1) {
				throw new RuntimeException("文章配置表插入失败");
			}
			ApArticleContent apArticleContent = new ApArticleContent();
			apArticleContent.setContent(content);
			apArticleContent.setArticleId(apArticle.getId());
			int insert2 = apArticleContentMapper.insert(apArticleContent);
			if (insert2 < 1) {
				throw new RuntimeException("文章配置插入失败");
			}
		} else {
			//	id存在修改
			
			//	修改article表
			int updateArticleResult = apArticleMapper.updateById(apArticle);
			if (updateArticleResult < 1) {
				throw new RuntimeException("修改文章失败");
			}
			//	修改article_content表
			ApArticleContent apArticleContent = new ApArticleContent();
			LambdaQueryWrapper<ApArticleContent> wrapper = new LambdaQueryWrapper<>();
			
			apArticleContentMapper.update(apArticleContent,wrapper);
			
			//	返回articleId
			
		}
		return ResponseResult.okResult("");
	}
	
	@Autowired
	private KafkaTemplate kafkaTemplate;
	
	@Override
	public void sendIndexUpdate(ApArticle article, String content) {
		if (article == null || StringUtils.isEmpty(content)) {
			log.warn("入参不能为空");
			return;
		}
		SearchArticleVo searchArticleVo = new SearchArticleVo();
		
		
		kafkaTemplate.send("es.index.update", searchArticleVo);
	}
	
	@Override
	public ResponseResult load(Short loadType, ArticleHomeDto dto) {
		List<ApArticle> articleList = apArticleMapper.loadArticle(loadType, dto);
		return ResponseResult.okResult(articleList);
	}
	@Override
	public ResponseResult load( ArticleHomeDto dto) {
		List<ApArticle> articleList = apArticleMapper.loadArticle(ArticleConstants.LOADTYPE_LOAD_MORE, dto);
		return ResponseResult.okResult(articleList);
	}
	
	@Override
	public ResponseResult load2(ArticleHomeDto dto, boolean firstPage) {
		if(firstPage){
			String jsonStr = cacheService.get(ArticleConstants.HOT_ARTICLE_FIRST_PAGE + dto.getTag());
			if(StringUtils.isNotBlank(jsonStr)){
				List<HotArticleVo> hotArticleVoList = JSON.parseArray(jsonStr, HotArticleVo.class);
				ResponseResult responseResult = ResponseResult.okResult(hotArticleVoList);
				return responseResult;
			}
		}
		return load(dto);
	}
}
